package com.cq.school.service;

import com.cq.school.entity.Article;
import com.cq.school.repository.ArticleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: huke
 * Date: 13-11-28
 * Time: 下午11:08
 * To change this template use File | Settings | File Templates.
 */
@Component
@Transactional
public class ArticleService {
    @Autowired
    private ArticleDao articleDao;

    public Page<Article> listArticle(int category,Map<String, Object> searchParams, int pageNumber, int pageSize){
        PageRequest pageRequest = buildPageRequest(pageNumber, pageSize);
        Specification<Article> spec = buildSpecification(category, searchParams);
        return articleDao.findAll(spec, pageRequest);
    }
    /**
     * 创建分页请求.
     */
    private PageRequest buildPageRequest(int pageNumber, int pagzSize) {
        return new PageRequest(pageNumber - 1, pagzSize, new Sort(Sort.Direction.DESC, "id"));
    }
    /**
     * 创建动态查询条件组合.
     */
    private Specification<Article> buildSpecification(int category, Map<String, Object> searchParams) {
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        if(category>0)
            filters.put("category", new SearchFilter("category", SearchFilter.Operator.EQ, category));
        Specification<Article> spec = DynamicSpecifications.bySearchFilter(filters.values(), Article.class);
        return spec;
    }

    public List<Article> listArticleByHits(int size){
        return articleDao.findAll(null,new PageRequest(0, size, new Sort(Sort.Direction.DESC, "hits"))).getContent();
    }

    public List<Article> listArticleByCreateTime(int size){
        return articleDao.findAll(null,new PageRequest(0, size, new Sort(Sort.Direction.DESC, "createTime"))).getContent();
    }

    public List<Article> listSchoolArticle(long schoolId,int size){
        return articleDao.listSchoolArticle(schoolId,size);
    }

    public List<Article> getNexPre(long id){
       return articleDao.getNexPre(id);
    }

    public void updateHits(long id){
        articleDao.updateHits(id);
    }


    public void saveArticle(Article article){
        if(article.getId()==null)
            article.setCreateTime(new Date());
        articleDao.save(article);
    }
    public Article getArticle(Long id){
        return articleDao.findOne(id);
    }
    public void deleteArticle(Long id){
        articleDao.delete(id);
    }
}
